# Contributor: Bart Ribbers <bribbers@disroot.org>
# Contributor: Grant Miller <GrantM11235@gmail.com>
# Contributor: Antoine Fontaine <antoine.fontaine@epfl.ch>
# Maintainer: Antoine Fontaine <antoine.fontaine@epfl.ch>
pkgname=anbox
pkgver=0_git20210902
pkgrel=2
_commit="31d584e13d24b1889566ae77e7cabc88e6414ebf"
_cpu_features_version="0.6.0"
pkgdesc="Android in a box"
url="https://github.com/anbox/anbox"
arch="x86_64 armv7 aarch64"
license="GPL-3.0-or-later"
subpackages="$pkgname-openrc"
install="$pkgname.post-install"
depends="
	fuse3
	iptables
	lxc
	mesa-egl
	mesa-gles
	"
makedepends="
	boost-dev
	cmake
	cmake-extras
	dbus-dev
	elfutils-dev
	elogind-dev
	glm-dev
	gtest-dev
	libcap-dev
	libdwarf-dev
	libexecinfo-dev
	lxc-dev
	mesa-dev
	properties-cpp-dev
	protobuf-dev
	python3
	sdbus-cpp-dev
	sdl2-dev
	sdl2_image-dev
	"
source="$pkgname-$_commit.tar.gz::https://github.com/anbox/anbox/archive/$_commit.tar.gz
	cpu_features-v$_cpu_features_version.tar.gz::https://github.com/google/cpu_features/archive/v$_cpu_features_version.tar.gz
	anbox-container-manager.initd
	anbox.confd
	no-bundled-sdbus.patch
	musl-fixes.patch
	give-more-time-to-start.patch
	https://github.com/ubports/anbox/commit/e34b59513422eb905b53d7dc6c0862cea535eb96.patch
	anbox-launch.sh
	anbox.desktop
	lxc4.patch
	apparmor.patch
	"
builddir="$srcdir/$pkgname-$_commit"

prepare() {
	# the bundled cpu_features is outdated and breaks build on arm.
	rm -r external/cpu_features
	cp -r $srcdir/cpu_features-*/ external/cpu_features

	default_prepare
}

build() {
	cmake -B build \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=OFF \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DANBOX_VERSION=alpine-$pkgver-r$pkgrel \
		-DWerror=OFF
	cmake --build build
}

check() {
	cd "$builddir"/build
	make test
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# Remove unnecessary files
	rm -r "$pkgdir"/usr/lib/backward/
	rm -r "$pkgdir"/usr/include

	install -m755 -D "$srcdir"/anbox-container-manager.initd \
		"$pkgdir"/etc/init.d/anbox-container-manager
	install -m644 -D "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/$pkgname
	install -m755 -D "$srcdir"/$pkgname-launch.sh \
		"$pkgdir"/usr/bin/$pkgname-launch

	install -m644 -D "$srcdir"/$pkgname.desktop \
		"$pkgdir"/usr/share/applications/$pkgname.desktop
	install -m644 -D "$builddir"/snap/gui/icon.png \
		"$pkgdir"/usr/share/icons/hicolor/512x512/anbox.png

	# this is a dependency of the init script
	install -m755 -D "$builddir"/scripts/anbox-bridge.sh \
		"$pkgdir"/usr/share/anbox/anbox-bridge.sh
	# this is for convenience, to allow someone to get root access
	install -m755 -D "$builddir"/scripts/anbox-shell.sh \
		"$pkgdir"/usr/share/anbox/anbox-shell.sh
}

sha512sums="
89afe367a67380b59163bf7025dcd1299db9494a6b82fe06a82b6150becb90915fa47c5f13d0e4f02379d95d1d5b434b6f2380d08fe932ea1e4b4470ae3bb287  anbox-31d584e13d24b1889566ae77e7cabc88e6414ebf.tar.gz
006a2e05253712cf605ecabccdda63dd9325445f8d145d5e2432c4342332e652f318810997321849be131082db435d88143020fdc85268fba204586cf37eef0d  cpu_features-v0.6.0.tar.gz
959058eb1c1e3eb0d1593695ee57fbc0cc567f260235e81d7b56da62b0b80fca9112d5f16a1e1c5c0e6223c8a1899f97f906c7cc763dc015e094aacded818a2f  anbox-container-manager.initd
6a3bc88142c5287ec54d481a4788eceb7772d9974af950b5286ce63a49d05d9d49fce5ba1d02b4b1c9893896fd4ba218fd4d39b8e640bdd61ad196b5d5c9a021  anbox.confd
c2b4ca0ff20df005abfbaa457d40ae65d3979353b1868fbfebff1c0527d1df71c5684a6b7dc9d71f114fa805c412501b1afc69b2e9e49d63b00671a8ebcb3d3c  no-bundled-sdbus.patch
401669509e05eb586ad40fc9e8f4284b52183f06c60e87bf4e923be165a72f10263daf226db7fd610ef6ff25d09abd140ca2d59187e2d1b85a0bd6ae887dd944  musl-fixes.patch
0f6324231028d32467dc95d43ed4865c29fde3396524479f514533e079179b0b068f1b38cbdf227af13d64ab117a27f3b962d8a99f5ecacf3fff4302d3ffe702  give-more-time-to-start.patch
049ef6e5d02bc1bb39c24d623b00c048443cfaba72637a22d27e202a42731d8e9e71cccd59c8f7dafc1816f1fd4508d337ec75f59891ebbdadc275b2daba466f  e34b59513422eb905b53d7dc6c0862cea535eb96.patch
921d69e28a2f63a6eaccdfbd6f53173d2e0c59dd650dd83c0d8f816b5f23f1ba61abb393b0cf5e48d80ce363df4f3e609de34a4e5a73c2b520dba96b76eda0c9  anbox-launch.sh
3e458e6215ecf0ac94d749f4d78e464850dcf2cc2b616b2f7ddf26e9617d63a90bd0c8be59e4540141a813af4bad84d0f84874105f7ef9de8149666fc3828da7  anbox.desktop
f10858bab173d6254efcdfe8ebea2e5717ec4a6bd7723d65c18e36fadbd06a8576786ad3c07fa6319cc23a6f18254d70ca34099cbcc633ea4341fc892c321610  lxc4.patch
51ef961527436aed33f817e37da8466e0b57da344233e9e5decec62bff7da45631f0b300dfc129bf2bd179143dfc900fa9bf9a64ac86c155712d8957d449fca4  apparmor.patch
"
